/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using Xceed.Utils.Licensing;
using Xceed.Silverlight.Compatibility;

namespace Xceed.Silverlight.Controls
{
  public class FieldNameList : IList, INotifyCollectionChanged
  {
    public bool IsFixedSize
    {
      get
      {
        return ( m_values != null );
      }
    }

    public bool IsReadOnly
    {
      get
      {
        return ( m_values != null );
      }
    }

    public string this[ int index ]
    {
      get
      {
        return m_values[ index ];
      }
      set
      {
        if( DesignerProperties.GetIsInDesignMode() && ( index == this.Count ) )
        {
          this.Add( value );
          return;
        }

        m_values[ index ] = value;
      }
    }

    public int Count
    {
      get
      {
        return m_values.Count;
      }
    }

    public object SyncRoot
    {
      get
      {
        return this;
      }
    }

    public bool IsSynchronized
    {
      get
      {
        return false;
      }
    }

    public int Add( string value )
    {
      if( string.IsNullOrEmpty( value ) || StringHelper.IsNullOrWhiteSpace( value ) )
        throw new ArgumentException( "Value must be a string and must not be null or empty.", "value" );

      if( this.Contains( value ) )
        throw new ArgumentException( "Value is already present in the list." );

      return ( ( IList )m_values ).Add( value );
    }

    public void Clear()
    {
      m_values.Clear();
    }

    public bool Contains( string value )
    {
      return m_values.Contains( value );
    }

    public int IndexOf( string value )
    {
      return m_values.IndexOf( value );
    }

    public void Insert( int index, string value )
    {
      if( string.IsNullOrEmpty( value ) || StringHelper.IsNullOrWhiteSpace( value ) )
        throw new ArgumentException( "Value must be a string and must not be null or empty.", "value" );

      if( this.Contains( value ) )
        throw new ArgumentException( "Value is already present in the list." );

      m_values.Insert( index, value );
    }

    public void Remove( string value )
    {
      if( string.IsNullOrEmpty( value ) || StringHelper.IsNullOrWhiteSpace( value ) )
        throw new ArgumentException( "Value must be a string and must not be null or empty.", "value" );

      m_values.Remove( value );
    }

    public void RemoveAt( int index )
    {
      m_values.RemoveAt( index );
    }

    public IEnumerator GetEnumerator()
    {
      return m_values.GetEnumerator();
    }

    public void CopyTo( Array array, int index )
    {
      ( ( ICollection )m_values ).CopyTo( array, index );
    }

    private ObservableCollection<string> m_values = new ObservableCollection<string>();

    #region INotifyCollectionChanged Implementation

    public event NotifyCollectionChangedEventHandler CollectionChanged
    {
      add
      {
        m_values.CollectionChanged += value;
      }
      remove
      {
        m_values.CollectionChanged -= value;
      }
    }

    #endregion

    #region IList Implementation

    int IList.Add( object value )
    {
      return this.Add( value as string );
    }

    void IList.Clear()
    {
      this.Clear();
    }

    bool IList.Contains( object value )
    {
      return this.Contains( value as string );
    }

    int IList.IndexOf( object value )
    {
      return this.IndexOf( value as string );
    }

    void IList.Insert( int index, object value )
    {
      this.Insert( index, value as string );
    }

    bool IList.IsFixedSize
    {
      get
      {
        return this.IsFixedSize;
      }
    }

    bool IList.IsReadOnly
    {
      get
      {
        return this.IsReadOnly;
      }
    }

    void IList.Remove( object value )
    {
      this.Remove( value as string );
    }

    void IList.RemoveAt( int index )
    {
      this.RemoveAt( index );
    }

    object IList.this[ int index ]
    {
      get
      {
        return this[ index ];
      }
      set
      {
        this[ index ] = value as string;
      }
    }

    void ICollection.CopyTo( Array array, int index )
    {
      this.CopyTo( array, index );
    }

    int ICollection.Count
    {
      get
      {
        return this.Count;
      }
    }

    bool ICollection.IsSynchronized
    {
      get
      {
        return this.IsSynchronized;
      }
    }

    object ICollection.SyncRoot
    {
      get
      {
        return this.SyncRoot;
      }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
      return this.GetEnumerator();
    }

    #endregion
  }
}
